package cn.bdqn;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTester2 {
    @Resource
    RedisTemplate redisTemplate;

    @Test
    public void test1(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
//        valueOperations.set("name","张三");;
//        Object name = valueOperations.get("name");
//        System.out.println(name);
//        //shijan
//        valueOperations.set("age",12,10, TimeUnit.SECONDS);
//        //修改
//        valueOperations.set("name","李四");
//        name = valueOperations.get("name");
//        System.out.println(name);
//        valueOperations.set("count",1);
//        valueOperations.increment("count",2);
        valueOperations.decrement("count",-2);

    }
    @Test
    public void test2(){
        HashOperations hashOperations = redisTemplate.opsForHash();
//        hashOperations.put("myhash","name","zhansan");
//        hashOperations.put("myhash","age",12);
//        hashOperations.put("myhash","sex","nan");
        //修改shanc
        hashOperations.put("myhash","age",18);
        hashOperations.delete("myhash","sex");
        //遍历
        Map<String,Object> myhash = hashOperations.entries("myhash");
        for (String o : myhash.keySet()) {
            System.out.println(o+" "+myhash.get(o));
        }
    }

    @Test
    public void test3(){
        ListOperations listOperations = redisTemplate.opsForList();
//        listOperations.leftPush("mylist","a");
//        listOperations.leftPushAll("mylist","s","m");
//        listOperations.rightPush("mylist","a");
//        listOperations.rightPushAll("mylist","c","b","d");
        listOperations.leftPop("mylist");
        listOperations.rightPop("mylist");
        List mylist = listOperations.range("mylist", 0, -1);
        for (Object o : mylist) {
            System.out.println(o);
        }
    }

    @Test
    public void test4(){
        SetOperations setOperations = redisTemplate.opsForSet();
//        setOperations.add("myset","a","a","b","c");
        setOperations.remove("myset","c");
        Set myset = setOperations.members("myset");
        for (Object o : myset) {
            System.out.println(o);
        }
    }
        @Test
    public void test5(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//       zSetOperations.add("myzset","张三",4);
//       zSetOperations.add("myzset","李四",3);
//       zSetOperations.add("myzset","王五",1);
//       zSetOperations.add("myzset","赵六",2);
    zSetOperations.remove("myzset","赵六");
        Set myzset = zSetOperations.range("myzset", 0, -1);
        for (Object o : myzset) {
            System.out.println(o);
        }
    }
}
